#!/usr/bin/env python
# -*- coding: utf-8 -*-
from vehicle.consts import RedisKey
from vehicle.models import VehicleInfoModel
from application.settings import redis_cli


class VehicleDal(object):

    def __init__(self):
        pass

    def get_vehicle_count(self):
        return VehicleInfoModel.objects.count()

    def get_vehicle_online_count(self):
        return VehicleInfoModel.objects.filter(vehicle_status=1).count()

    def get_mileage(self):
        mileage = redis_cli.mget(*RedisKey.get_mileage_key())
        mileage = [round(float(d.decode()), 2) if d else 0 for d in mileage]
        return dict(
            daily_mileage=mileage[0],
            weekly_mileage=mileage[1],
            monthly_mileage=mileage[2],
            total_mileage=mileage[3],
        ) 
